/**
 * Created with WebStorm.
 * @Author: panda
 * @Date: 2022-05-18 09:26
 * @Description: 用户接口
 */
import {APISchema} from './axios/type';
import {createRequestClient} from './axios/request';
import localCache from "@/utils/localCache";

const token = localCache.getCache('token');

interface UserSchema extends APISchema {
  userLogin: {
    request: {
      username: string;
      passwordHash: string;
    }
    response: {
      code: number,
      data: {
        token: string,
        userInfoVo: {
          avatar: string,
          id: string,
          sex: string,
          username: string,
          detail: string
          nickname: string, email: string
        }
      },
      msg: string
    }

  };
  userRegister: {
    request: {
      username: string,
      passwordHash: string,
      captchaCode: string, captchaId: string
    };
    response: {
      code: number,
      data: {},
      msg: string
    }
  };
  logout: {
    request: {};
    response: {
      code: number,
      data: {},
      msg: string
    };
  };
  getUserInfo: {
    request: { id: string | string[] };
    response: {
      code: number,
      data: {
        id: string;
        username: string;
        nickname: string;
        sex: string;
        avatar: string;
        detail: string;
      },
      msg: string
    };
  };
  getArticles: {
    request: { id: number, pageNum: number, pageSize: number, categoryId: number | undefined };
    response: {
      code: number,
      data: {
        rows: [
          {
            id: number,
            title: string,
            content: string,
            thumbnail: string,
            views: number,
            categoryId: string,
            categoryName: string,
            createTime: string,
            likes: number,
          }
        ],
        total: number
      },
      msg: string
    };
  };
  getVideos: {
    request: { id: number, pageNum: number, pageSize: number, categoryId: undefined };
    response: {
      code: number,
      data: {
        rows: [
          {
            copyright: string,
            cover: string,
            createBy: string,
            createTime: string,
            categoryId: number
            detail: string,
            duration: number,
            id: number,
            likes: number,
            title: string,
            videoUrl: string,
            views: number,
            username: string,
            avatar: string,
            videoSize: number
          }
        ],
        total: number
      },
      msg: string
    };
  };
  updateUserInfo: {
    request: {avatar: string, sex: string, detail?: string,username:string
    };
    response: {
      code: number,
      data: {
        username: string, nickname: string, email: string, avatar: string, sex: string, detail: string, id: string
      }
      msg: string
    };
  };
  updateUsername: {
    request: {
      avatar: string, sex: string, detail?: string,username:string
    };
    response: {
      code: number,
      data: {
        username: string, nickname: string, email: string, avatar: string, sex: string, detail: string, id: string
      }
      msg: string
    };
  };
  changePassword: {
    request: {
      oldPwd: string
      newPwd: string
    };
    response: {
      code: number,
      data: {

      }
      msg: string
    };
  }
}

export const userApi = createRequestClient<UserSchema>({
  baseURL: process.env.NODE_ENV === 'development'
    ? process.env.VUE_APP_API_HOST + ''
    : '',
  apis: {
    userLogin: 'POST /login',
    userRegister: 'POST /register',
    logout: {
      path: '/logout',
      method: 'POST',
      headers: {'token': token}
    },
    getUserInfo: 'GET /users/:id',
    getArticles: 'GET /users/:id/articles',
    getVideos: 'GET /users/:id/videos',
    updateUserInfo: {
      path: 'PUT /users',
      method: 'PUT',
      headers: {'token': token}
    },
    updateUsername:{
      path: 'PUT /users/username',
      method: 'PUT',
      headers: {'token': token}
    },
    changePassword: {
      path: 'PUT /users/password',
      method: 'PUT',
      headers: {'token': token}
    }
  }
});
